import './style.scss'
import { number2color, ruleModalSetting, strokeWidthSetting, readFile } from './utils'

const strokeWidth = strokeWidthSetting()
ruleModalSetting()

const map = new BMapGL.Map('Map', {
    // center: [116.397428, 39.90923],
    // zoom: 11,
});
map.centerAndZoom(new BMapGL.Point(116.397428, 39.90923), 11);  // 初始化地图,设置中心点坐标和地图级别
map.enableScrollWheelZoom(true);

function parseGeo(e) {
    var t = e.split("|"),
        n = parseInt(t[0]),
        r = t[1],
        i = t[2],
        a = i.split(";");
    if (4 === n) {
        for (var o = [], s = 0; s < a.length - 1; s++) 0 === a[s].indexOf("1-") && o.push(a[
            s].substr(2, a[s].length - 2));
        a = o, a.push("")
    }
    var u = [];
    switch (n) {
        case 1:
            u.push(a[0]);
            break;
        case 2:
        case 3:
        case 4:
            for (var s = 0; s < a.length - 1; s++) {
                var l = a[s];
                if (l.length > 100) l = l.replace(
                    /(-?[1-9]\d*\.\d*|-?0\.\d*[1-9]\d*|-?0?\.0+|0|-?[1-9]\d*),(-?[1-9]\d*\.\d*|-?0\.\d*[1-9]\d*|-?0?\.0+|0|-?[1-9]\d*)(,)/g,
                    "$1,$2;"), u.push(l);
                else {
                    for (var c = [], d = l.split(","), f = 0; f < d.length; f += 2) {
                        var h = d[f],
                            m = d[f + 1];
                        c.push(h + "," + m)
                    }
                    u.push(c.join(";"))
                }
            }
    }
    return u.length <= 1 && (u = u.toString()), {
        type: n,
        bound: r,
        points: u
    }
}
function parsePoint(list) {
    return list.map(item => item.split(';').map(i => i.split(',')))
}

function canvasLine(ls, color = 'blue') {
    let path = []
    for (let i of ls) {
        path.push(new BMapGL.Point(...map.mercatorToLnglat(...i)))
    }
    console.log(path)
    var polyline = new BMapGL.Polyline(path, {
        strokeColor: color,
        strokeWeight: strokeWidth.value,
        strokeOpacity: 1
    })
    // 将折线添加至地图实例
    map.addOverlay(polyline);
}

function init(list) {
    for (let item of list) {
        if (item.C) {
            console.log(parseGeo(item.C))
            const { points } = parseGeo(item.C)
            let list = parsePoint(points)
            console.log(list)
            for (let path of list) {
                canvasLine(path, number2color(item.B))
            }
        }
    }
}

document.querySelector('#Upload').addEventListener('change', e => {
    const file = e.target.files[0]
    readFile(file).then(res => {
        console.log(res)
        init(res)
    })
});


/*
(function () {
    function addRoute(e) {
        var i, r, s, d, _, l;
        i = e.split(";")
        l = i.length - 1
        const cc = map.mercatorToLnglat(
            parseFloat(i[0].split(",")[0]),
            parseFloat(i[0].split(",")[1])
        )
        console.log(cc)
        r = new BMapGL.Point(
            parseFloat(i[0].split(",")[0]),
            parseFloat(i[0].split(",")[1])
        )
        s = new BMapGL.Point(
            parseFloat(i[l].split(",")[0]),
            parseFloat(i[l].split(",")[1])
        )
        return { i, l, r, s }
    }
    function parseGeo(e) {
        if ("string" == typeof e) {
            var t = e.split("|"),
                n = parseInt(t[0]),
                r = t[1],
                i = t[2],
                a = i.split(";");
            if (4 === n) {
                for (var o = [], s = 0; s < a.length - 1; s++) 0 === a[s].indexOf("1-") && o.push(a[
                    s].substr(2, a[s].length - 2));
                a = o, a.push("")
            }
            var u = [];
            switch (n) {
                case 1:
                    u.push(a[0]);
                    break;
                case 2:
                case 3:
                case 4:
                    for (var s = 0; s < a.length - 1; s++) {
                        var l = a[s];
                        if (l.length > 100) l = l.replace(
                            /(-?[1-9]\d*\.\d*|-?0\.\d*[1-9]\d*|-?0?\.0+|0|-?[1-9]\d*),(-?[1-9]\d*\.\d*|-?0\.\d*[1-9]\d*|-?0?\.0+|0|-?[1-9]\d*)(,)/g,
                            "$1,$2;"), u.push(l);
                        else {
                            for (var c = [], d = l.split(","), f = 0; f < d.length; f += 2) {
                                var h = d[f],
                                    m = d[f + 1];
                                c.push(h + "," + m)
                            }
                            u.push(c.join(";"))
                        }
                    }
            }
            return u.length <= 1 && (u = u.toString()), {
                type: n,
                bound: r,
                points: u
            }
        }
    }

    const str = '2|12955927.37,4810591.31;12956543.75,4810670.96|12955927.37,4810591.31,12955935.60,4810592.22;12956127.36,4810613.57,12956141.74,4810616.37;12955935.60,4810592.22,12956127.36,4810613.57;12956141.74,4810616.37,12956192.31,4810620.91;12956192.31,4810620.91,12956370.44,4810640.38,12956507.37,4810651.14,12956532.75,4810662.86,12956543.75,4810670.96;'
    console.log(parseGeo(str))
    parseGeo(str).points.forEach(item => {
        console.log(addRoute(item))
    })
})

*/